package com.haitonghop.modules.news.rssreader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.util.Log;

import com.haitonghop.modules.news.rssreader.exception.EndSAXException;
import com.haitonghop.modules.news.rssreader.model.Feed;
import com.haitonghop.network.Request;

/**
 * Library main class. You need to create a new instance to operate with
 *
 * @author ffernandez
 *
 */
public class SimpleFeedReader {

	private XMLReader xmlReader;

	/**
	 * Constructor. It initializes the {@code XMLReader}
	 */
	public SimpleFeedReader() {

		try {
			shutDownAll();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser;
		try {
			parser = factory.newSAXParser();
			xmlReader = parser.getXMLReader();
		} catch (ParserConfigurationException e) {
			System.err.println(e.getMessage());
		} catch (SAXException e) {
			System.err.println(e.getMessage());
		}
	}

	/**
	 * Parse feed
	 *
	 * @param feedURLObject
	 *            the feed url
	 * @return the feed without entries summary
	 * @see #getFeed(URL, Date, boolean)
	 */
	public Feed getFeed(URL feedURLObject) {
		return getFeed(feedURLObject, null, false);
	}

	/**
	 * Parse feed and return only those entries with a publication date before
	 * the specified date
	 *
	 * @param feedURLObject
	 *            the feed url
	 * @param lastUpdatedDate
	 * @return the feed without entries summary
	 * @see #getFeed(URL, Date, boolean)
	 */
	public Feed getFeed(URL feedURLObject, Date lastUpdatedDate) {
		return getFeed(feedURLObject, lastUpdatedDate, false);
	}

	/**
	 * Parse feed with the entries summary if the boolean parameter is true
	 *
	 * @param feedURLObject
	 *            the feed url
	 * @param summary
	 * @return the feed
	 * @see #getFeed(URL, Date, boolean)
	 */
	public Feed getFeed(URL feedURLObject, boolean summary) {
		return getFeed(feedURLObject, null, false);
	}

	/**
	 * Parse feed with the entries summary if the boolean parameter is true.
	 * Return only those entries with a publication date before the specified
	 * date
	 *
	 * @param feedURLObject
	 *            the feed url
	 * @param lastUpdatedDate
	 * @param summary
	 * @return the feed
	 * @throws IOException
	 * @see #getFeed(URL, Date, boolean)
	 */

	public static void shutDownAll() throws IOException {
		if (httpClient != null) {
			httpClient.getConnectionManager().shutdown();
		}
		if (httpGet != null) {
			httpGet.abort();
		}
		if (is != null) {
			is.close();
		}
	}

	private static String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	private static HttpParams httpParameters;
	private static HttpClient httpClient;
	private static HttpGet httpGet;
	private static InputStream is;

	public Feed getFeed(URL feedURLObject, Date lastUpdatedDate, boolean summary) {
		Log.i("getFeed", "1 start");
		FeedHandler feedHandler = new FeedHandler(lastUpdatedDate, summary);
		if (xmlReader != null && feedURLObject != null) {
			httpClient = new DefaultHttpClient(httpParameters);
			Log.i("getFeed", "2 new DefaultHttpClient");
			try {

				xmlReader.setContentHandler(feedHandler);
				httpClient = new DefaultHttpClient();
				httpClient.getParams().setParameter(Request.AGENT, Request.FIREFOX);

				HttpResponse response = httpClient.execute(new HttpGet(feedURL));
				StatusLine statusLine = response.getStatusLine();
				if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
					is = response.getEntity().getContent();
					xmlReader.parse(new InputSource(is));

				} else {
					// Closes the connection.
					response.getEntity().getContent().close();
					throw new IOException(statusLine.getReasonPhrase());
				}

			} catch (EndSAXException e) {
				// No problem
				try {
					shutDownAll();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} catch (IOException e) {
				System.err.println(e.getMessage());
				try {
					shutDownAll();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} catch (SAXException e) {
				System.err.println(e.getMessage());
				try {
					shutDownAll();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						System.err.println(e.getMessage());
					}
				}
				httpClient.getConnectionManager().shutdown();
			}
		}
		return feedHandler.getFeed();
	}

	/**
	 * Parse feed
	 *
	 * @param feedURL
	 *            the feed url
	 * @return the feed without entries summary
	 * @see #getFeed(String, Date, boolean)
	 */
	public Feed getFeed(String feedURL) {
		return getFeed(feedURL, null, false);
	}

	/**
	 * Parse feed and return only those entries with a publication date before
	 * the specified date
	 *
	 * @param feedURL
	 *            the feed url
	 * @param lastUpdatedDate
	 * @return the feed without entries summary
	 * @see #getFeed(String, Date, boolean)
	 */
	public Feed getFeed(String feedURL, Date lastUpdatedDate) {
		return getFeed(feedURL, lastUpdatedDate, false);
	}

	/**
	 * Parse feed with the entries summary if the boolean parameter is true
	 *
	 * @param feedURL
	 *            the feed url
	 * @param summary
	 * @return the feed
	 * @see #getFeed(String, Date, boolean)
	 */
	public Feed getFeed(String feedURL, boolean summary) {
		return getFeed(feedURL, null, summary);
	}

	/**
	 * Parse feed with the entries summary if the boolean parameter is true.
	 * Return only those entries with a publication date before the specified
	 * date
	 *
	 * @param feedURL
	 *            the feed url
	 * @param lastUpdatedDate
	 * @param summary
	 * @return the feed
	 * @see #getFeed(URL, Date, boolean)
	 */
	private String feedURL;

	public Feed getFeed(String feedURL, Date lastUpdatedDate, boolean summary) {
		URL feedURLObject = null;
		try {
			feedURLObject = new URL(feedURL);
			this.feedURL = feedURL;
		} catch (MalformedURLException e) {
			System.err.println(e.getMessage());
		}
		return getFeed(feedURLObject, lastUpdatedDate, summary);
	}

	private static String getFirstLine(InputStream is) {
		if (is != null) {
			String line = null;
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(is), 1024);
				line = reader.readLine();
			} catch (IOException e) {
				//
			}
			return line;
		} else {
			return "";
		}
	}

	private static String detectEncoding(URL urlObj) {
		String encoding = null;
		try {
			URLConnection connection = urlObj.openConnection();
			encoding = connection.getContentEncoding();
			if (encoding == null || encoding.equalsIgnoreCase("none")) {
				InputStream is = connection.getInputStream();
				String line = getFirstLine(is).toLowerCase();
				Pattern pattern = Pattern.compile("encoding=[\"']([\\w\\-_]+)[\"']");
				Matcher matcher = pattern.matcher(line);
				if (matcher.find()) {
					encoding = matcher.group(1);
				}
				is.reset();
			}
		} catch (MalformedURLException e) {
			//
		} catch (IOException e) {
			//
		}
		return encoding;
	}

}
